import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import layout from '../layout/index.vue'
import store from '../store'
/**
 * 私有路由表
 */
export const publicRoutes: Array<RouteRecordRaw> = [
  {
    path: '/login',
    component: () => import('@/views/login/index.vue')
  },
  {
    path: '/',
    redirect: '/profile',
    component: layout,
    children: [
      // 个人中心
      {
        path: '/profile',
        name: 'profile',
        component: () => import('@/views/profile/index.vue'),
        meta: {
          title: 'profile',
          icon: 'el-icon-user'
        }
      },
      // 401
      {
        path: '/401',
        name: '401',
        component: () => import('@/views/errorPage/401.vue')
      },
      // 404
      {
        path: '/404',
        name: '404',
        component: () => import('@/views/errorPage/404.vue')
      },
      // 500
      {
        path: '/500',
        name: '500',
        component: () => import('@/views/errorPage/500.vue')
      }
    ]
  }
]
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: publicRoutes
})
/**
 *  初始化路由表
 */
export function resetRouter() {
  if (
    store.getters.userInfo &&
    store.getters.userInfo.permission &&
    store.getters.userInfo.permission.menu
  ) {
    store.getters.userInfo.permission.menu.forEach((item: any) => {
      router.removeRoute(item.name)
    })
  }
}
export default router
